// packageI/donation_project/donationProjectDetails/donationProjectDetails.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    month_arr:[1,2,3,4,5,6,7,8,9,10,11,12],
    time_show:false,
    time_text:'',
    current_year:'',
    current_month:'',
    max_year:'',
    max_month:'',

    person: {},
    info_list: [],
    picture_list: [],
    video_list: [],
    pic_src_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date = new Date();
    
    this.setData({
      current_year : date.getFullYear(),
      max_year : date.getFullYear(),
      current_month : (date.getMonth()+1),
      max_month : (date.getMonth()+1)
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  handImagePreview(evt){
    let src = evt.currentTarget.dataset.src;
    wx.previewImage({
      urls:[src]
    });
  },
  getData(){
    let json={id:this.options.id};
    if (this.data.time_text != "") {
      json.time = this.data.time_text;
    }
    let urlStr = app.getNetAddresss("plugin.present-project.frontend.project.personDetail");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        
        let info_list = [],
          picture_list = [],
          video_list = [],
          pic_src_list = [];
        res.data.person_list.forEach(element => {
          if (info_list.length < 5) {
            let project_title = "";
            if (element.project_v && element.project_v.title) {
              project_title = element.project_v.title;
            }
            let o = { id: element.id, price: element.price, remarks: element.remarks, project_title };
            info_list.push(o);
          }
          if (picture_list.length < 10) {
            element.change_img.forEach(item => {
              if (picture_list.length == 10) return;
              let project_title = "";
              if (element.project_v && element.project_v.title) {
                project_title = element.project_v.title;
              }
              let o = { name: project_title, url: item };
              picture_list.push(o);
              pic_src_list.push(item);
            });
          }
  
          if (video_list.length < 10) {
            element.change_video.forEach(item => {
              if (video_list.length == 10) return;
              let project_title = "";
              if (element.project_v && element.project_v.title) {
                project_title = element.project_v.title;
              }
              let o = { name: project_title, url: item };
              video_list.push(o);
            });
          }
        });

        this.setData({
          person:res.data.person,
          info_list,picture_list,
          video_list,pic_src_list
        });

      }
    });
  },

  changeYear(evt){
    let type = evt.currentTarget.dataset.type;
    if(type=='add'){
      if(this.data.current_year<this.data.max_year){
        let current_year = ++this.data.current_year;
        this.setData({
          current_year:current_year
        });
        if(this.data.current_year == this.data.max_year){
          this.setData({
            current_month:this.data.max_month
          });
        }
      }
    }else if(type=='recude'){
      let current_year = --this.data.current_year;
      if(this.data.current_year>1){
        this.setData({
          current_year:current_year
        });
      }
    }
  },
  changeMonth(evt){
    let item  = evt.currentTarget.dataset.item;
    if(this.data.current_year>=this.data.max_year){
      if(item<=this.data.max_month){
        this.setData({current_month:item});
      }
    }else {
      this.setData({current_month:item});
    }
  },
  timeConfirm(){
    this.setData({
      time_show:false,
      time_text:( this.data.current_year+'-'+this.data.current_month)
    });
    this.getData();
  },

  gotoPage(evt){
    let name = evt.currentTarget.dataset.name;
    let url = `/packageI/donation_project/${name}/${name}?id=${this.options.id}`;
    if(this.data.time_text!=''){
      url+=`&time=${this.data.time_text}`;
    }
    wx.navigateTo({url});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  setTimeShow(){
    this.setData({
      time_show:!this.data.time_show
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});